package com.bigpig245.mangareader;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.bigpig245.mangareader.entity.AbsImageLink;
import com.bigpig245.mangareader.utils.ImageUtils;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class ImageLinkActivity extends Activity {

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private ViewFlipper mViewFlipper;
	private AnimationListener mAnimationListener;
	private Context mContext;

	@SuppressWarnings("deprecation")
	private final GestureDetector detector = new GestureDetector(
			new SwipeGestureDetector());

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		setContentView(R.layout.image_slider);
		mContext = this;
		mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);

		List<AbsImageLink> list = new ArrayList<AbsImageLink>();

		for (int i = 0; i < 8; i++) {
			list.add(new AbsImageLink("001", ""));
			Drawable d = ImageUtils.getImageFromURL(
					"http://z.mfcdn.net/store/manga/10543/TBD-240.0/compressed/j00"
							+ (i + 1) + ".jpg", "test");
			setFlipperImage(d);
			// add source
		}

		mViewFlipper.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(final View view, final MotionEvent event) {
				detector.onTouchEvent(event);
				return true;
			}
		});

		// animation listener
		mAnimationListener = new Animation.AnimationListener() {
			public void onAnimationStart(Animation animation) {
				// animation started event
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
			}
		};
	}

	class SwipeGestureDetector extends SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			try {
				// right to left swipe
				if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
						&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
					mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
							mContext, android.R.anim.fade_in));
					mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
							mContext, android.R.anim.fade_out));
					// controlling animation
					mViewFlipper.getInAnimation().setAnimationListener(
							mAnimationListener);
					mViewFlipper.showNext();
					return true;
				} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
						&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
					mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
							mContext, android.R.anim.fade_in));
					mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
							mContext, android.R.anim.fade_out));
					// controlling animation
					mViewFlipper.getInAnimation().setAnimationListener(
							mAnimationListener);
					mViewFlipper.showPrevious();
					return true;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}

			return false;
		}
	}

	private void setFlipperImage(Drawable d) {
		ImageView image = new ImageView(getApplicationContext());
		image.setImageDrawable(d);
		mViewFlipper.addView(image);
	}

	private String[] mStrings = {
			"http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
			"http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
			"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
			"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
			"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook.png",
			"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
			"http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
			"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
			"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
			"http://a3.twimg.com/profile_images/548410609/icon_8_73.png",
			"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
			"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
			"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android_normal.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/121630227/Droid.jpg",
			"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
			"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
			"http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter_normal.png",
			"http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
			"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
			"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
			"http://a1.twimg.com/profile_images/97470808/icon.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
			"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
			"http://a1.twimg.com/profile_images/74724754/android_logo.png",
			"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
			"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
			"http://a3.twimg.com/profile_images/548410609/icon_8_73_normal.png",
			"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
			"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
			"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
			"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
			"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
			"http://a1.twimg.com/profile_images/957149154/twitterhalf.jpg",
			"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
			"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
			"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
			"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
			"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
			"http://a1.twimg.com/profile_images/349012784/android_logo_small.jpg",
			"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
			"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
			"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png" };

}
